import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:netflix_frontend/common/helper/message/display_message.dart';
import 'package:netflix_frontend/common/helper/navigation/app_navigation.dart';
import 'package:netflix_frontend/core/configs/theme/app_colors.dart';
import 'package:netflix_frontend/data/auth/models/login_req_params.dto.dart';
import 'package:netflix_frontend/domain/auth/usecases/login.dart';
import 'package:netflix_frontend/presentation/auth/pages/signup.page.dart';
import 'package:netflix_frontend/presentation/home/pages/home.page.dart';
import 'package:netflix_frontend/service_locator.dart';
import 'package:reactive_button/reactive_button.dart';

class LoginPage extends StatelessWidget {
  LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        minimum: const EdgeInsets.only(top: 100, right: 16, left: 16),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _loginText(),
            const SizedBox(
              height: 30,
            ),
            _emailField(),
            const SizedBox(
              height: 20,
            ),
            _passwordField(),
            const SizedBox(
              height: 60,
            ),
            _loginButton(context),
            const SizedBox(
              height: 20,
            ),
            _signupText(context)
          ],
        ),
      ),
    );
  }

  Widget _loginText() {
    return const Text(
      "Log In",
      style: TextStyle(
        fontWeight: FontWeight.bold,
        fontSize: 24,
      ),
    );
  }

  Widget _emailField() {
    return TextField(
      controller: _emailCon,
      decoration: const InputDecoration(
        hintText: 'Email',
      ),
    );
  }

  Widget _passwordField() {
    return TextField(
      controller: _passwordCon,
      decoration: const InputDecoration(
        hintText: 'Password',
      ),
    );
  }

  Widget _signupText(BuildContext context) {
    return Text.rich(
      TextSpan(
        children: [
          const TextSpan(text: "Don't have account yet?"),
          TextSpan(
              text: 'Sign Up',
              style: const TextStyle(
                color: Colors.blue,
              ),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  AppNavigator.push(context, SignupPage());
                }),
        ],
      ),
    );
  }

  Widget _loginButton(BuildContext context) {
    return ReactiveButton(
      title: 'Log In',
      activeColor: AppColors.primary,
      onPressed: () async => sl<LoginUsecase>().call(
        params: LoginReqParams(
          email: _emailCon.text,
          password: _passwordCon.text,
        ),
      ),
      onSuccess: () {
        // TODO: add const prefix HomePage()
        AppNavigator.pushAndRemove(context, HomePage());
      },
      onFailure: (error) {
        DisplayMessage.errorMessage(error, context);
      },
    );
  }

  final TextEditingController _emailCon = TextEditingController();
  final TextEditingController _passwordCon = TextEditingController();
}
